<script src="/js/lib.js" type="text/javascript"></script>
<script src="/js/extjs-ux/app/ext-searchfield.js" type="text/javascript"></script>
<script src="/js/extjs-ux/form/ext-multiselect.js" type="text/javascript"></script>
<?php $translate=Zend_Registry::get('translate');?>
<script language="javascript">
Ext.onReady(function(){	
	function rowFormat(value, p, record) {
		var style='';
		if(record.data.active == 0) {style = 'color:red;'};
		if(record.data.del == 1) {style = 'text-decoration:line-through'};
		return String.format(
        	'<div style='+style+'><b>[{0}]: {1} {2}</b><br><span>{3}</span></div>',
        	value,record.data.firstname, record.data.lastname,record.data.address
        );
	}


	function doInsert(){
		addWin.show();
	}

	function doEdit() {		
		var m = myGrid.getSelectionModel().getSelections(); // get selected row of grid
		if(m.length > 0) {
			editWin.show();
			Ext.getCmp("editForm").getForm().load({ 
				url: '/user/index/primarykey/',
				params:{uid: m[0].get('id')},
				waitMsg: 'Loading'
			});
		}
		else
			Ext.MessageBox.alert("<?php echo $translate->translate("Notice")?>", "<?php echo $translate->translate("Select data you want to edit")?>");
	}
	

	function doDelete() {		
		var m = myGrid.getSelectionModel().getSelections();
		if(m.length > 0) {
			Ext.MessageBox.confirm(
				'<?php echo $translate->translate("Notice")?>',
				'<?php echo $translate->translate("Are you sure you want to delete")?>',
				function(btn) {
					if (btn == 'yes') {
						Ext.Ajax.request({
							url: '/user/delete/', 
							params : {uid: m[0].get('id')},
							method: 'POST',
							success: function (result, request){
								var res = new Object();
								res = Ext.util.JSON.decode(result.responseText);
								if (res.success == false)
									Ext.MessageBox.alert('<?php echo $translate->translate("Notice")?>', '<?php echo $translate->translate("Cannot delete members")?> '+ m[0].get("username")+'!');						
								else
								{
									Ext.Message.msg('<?php echo $translate->translate("Notice")?>', '<?php echo $translate->translate("Deleted data")?>');
									myGrid.getStore().reload();
								}
							}
						});	
					}
				}					
			);					
		}
		else
			Ext.MessageBox.alert('<?php echo $translate->translate("Notice")?>', '<?php echo $translate->translate("Select data you want to delete")?>');
	}	

	function doLock(){
		var m = myGrid.getSelectionModel().getSelections(); 
		if(m.length > 0) {
			if(m[0].get('active')==1){
				Ext.Ajax.request({
					url: '/user/lock/', 
					params : {uid: m[0].get('id')},
					method: 'POST',
					success: function (result, request){
						var res = new Object();
						res = Ext.util.JSON.decode(result.responseText);
						if (res.success == false)
							Ext.MessageBox.alert('<?php echo $translate->translate("Notice")?>', '<?php echo $translate->translate("Cannot lock members")?> '+ m[0].get("username") + '!');						
						else
						{
							Ext.Message.msg('<?php echo $translate->translate("Notice")?>', '<?php echo $translate->translate("Locked member")?>');
							myGrid.getStore().reload();
						}
					}
				});
			}
			else
				Ext.MessageBox.alert('<?php echo $translate->translate("Notice")?>', '<?php echo $translate->translate("Cannot lock members")?>');
		}
		else
		{
			Ext.MessageBox.alert('<?php echo $translate->translate("Notice")?>', '<?php echo $translate->translate("Select members")?>');
		}	
	}

	function doUnlock(){
		var m = myGrid.getSelectionModel().getSelections(); 
		if(m.length > 0) {
			if(m[0].get('active')==0){
				Ext.Ajax.request({
					url: '/user/unlock/', 
					params : {uid: m[0].get('id')},
					method: 'POST',
					success: function (result, request){
						var res = new Object();
						res = Ext.util.JSON.decode(result.responseText);
						if (res.success == false)
							Ext.MessageBox.alert('<?php echo$translate->translate("Notice") ?>', '<?php echo$translate->translate("Cannot unlock members") ?> '+ m[0].get('username') + '!');						
						else
						{
							Ext.Message.msg('<?php echo$translate->translate("Notice") ?>', '<?php echo$translate->translate("Unlocked member") ?>');
							myGrid.getStore().reload();
						}
					}
				});
			}
			else
				Ext.MessageBox.alert('<?php echo$translate->translate("Notice") ?>', '<?php echo $translate->translate("Cannot unlock members") ?>');
		}
		else
			Ext.MessageBox.alert('<?php echo $translate->translate("Notice") ?>', '<?php echo $translate->translate("Select members") ?>');
	}

	function btnEdit_Click(){
		editFrom.getForm().submit({
            waitMsg: 'Saving...',
            success: function (){
                editFrom.getForm().reset();
                editWin.hide();
                myGrid.getStore().reload();
                Ext.Message.msg('<?php echo $translate->translate("Notice") ?>', '<?php echo $translate->translate("Successfully updated") ?>');
            },
            failure: function(form, action) {
               var res = new Object();
			   res = Ext.util.JSON.decode(action.response.responseText);
			   Ext.MessageBox.alert('<?php echo $translate->translate("Notice") ?>', res.data.errors);
            }
        });
	}

	function btnAdd_Click(){
		addFrom.getForm().submit({ 
	  		method:'POST',
	  		waitMsg: 'Saving...',
	  		success: function () {
		  		addFrom.getForm().reset();
                addWin.hide();
                myGrid.getStore().reload();
                Ext.Message.msg('<?php echo $translate->translate("Notice")?>', '<?php echo $translate->translate("Add new success")?>');
            },
            failure:function(form, action){
            	if(action.failureType == 'server'){
                    obj = Ext.util.JSON.decode(action.response.responseText);
					Ext.Msg.alert('<?php echo $translate->translate("Notice")?>', obj.data.errors);
				}
            }
        });
	}
	
	<?php echo $this->layout?>
});
</script>